/*
 * Timer.cpp
 *
 *  Created on: 24.05.2011
 *      Author: aaw707
 */

#include "Timer.h"

using namespace std;

Timer::Timer(int timeSpanS, int intervalS, int timeSpanNS, int intervalNS, Timer_art timerArt){
	timeSpanS_ = timeSpanS;
	intervalS_ = intervalS;
	timeSpanNS_ = timeSpanNS;
	intervalNS_ = intervalNS;
	timerArt_ = timerArt;

	Starten();
}

Timer::~Timer() {
	timer_delete( timerid );
}

// Definiere Pulse Event

void Timer::Starten(){

	SIGEV_PULSE_INIT (&event, coid, SIGEV_PULSE_PRIO_INHERIT, TIMER, timerArt_);

	timer_create (CLOCK_REALTIME, &event, &timerid); // Fehlerbehandlung fehlt nach wie vor

	timer.it_value.tv_sec = timeSpanS_;
	timer.it_value.tv_nsec = timeSpanNS_;
	timer.it_interval.tv_sec = intervalS_;
	timer.it_interval.tv_nsec = intervalNS_;
	timer_settime (timerid, 0, &timer, NULL);

	gettimeofday(&zeit, 0);
	start_time = zeit.tv_usec + ((zeit.tv_sec)*1000000);

	//cout << "****************** start_time: " << start_time << "; timeSpanNS_: " << timeSpanNS_ << "; timeSpanS_: " << timeSpanS_ << "; timerArt_: " << timerArt_ << endl;

}

long long Timer::Stoppen(){

	long long komplett_zeit = (long long)((timeSpanS_ * 1000000) + (timeSpanNS_ / 1000));
	//cout << "Timer::Stoppen: komplett_zeit --------->" << komplett_zeit << endl;

	gettimeofday(&zeit, 0);

	//cout << "Timer::Stoppen: ZEIT: " << (komplett_zeit - ((zeit.tv_usec + (zeit.tv_sec * 1000000)) - start_time)) << endl;
	return (komplett_zeit - ((zeit.tv_usec + (zeit.tv_sec * 1000000)) - start_time));
}
